package com.servlet.music;

import com.dao.ManagerService;
import com.dao.SongService;
import com.dao.impl.ManagerServiceImpl;
import com.dao.impl.SongServiceImpl;
import com.entity.Manager;
import com.entity.Song;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.servlet.teacher.BaseServlet;
import com.util.JWTTokenUtil;
import com.util.JsonUtil;
import com.util.ProxyUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/MannagerServlet")
public class MannagerServlet extends BaseServlet {

	/**
	 * 管理员登录
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		ManagerService managerService = (ManagerService) ProxyUtil.getDaoProxy(new ManagerServiceImpl());

		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String token = null;
		try {
			if(username == null || password == null) {
				throw new RuntimeException("账号或密码为空");
			}

			boolean flag = managerService.login(username, password);

			if(!flag) throw new RuntimeException("密码不正确");

			// 返回token
			JWTTokenUtil jwtTokenUtil = new JWTTokenUtil();
			token = jwtTokenUtil.getEncodeToken(username);

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		jsonUtil.setData(token);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 * 获取管理员登录信息
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		JWTTokenUtil jwtTokenUtil = new JWTTokenUtil();
		String username = jwtTokenUtil.getDecodeToken(req);

		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		ManagerService managerService = (ManagerService) ProxyUtil.getDaoProxy(new ManagerServiceImpl());

		Manager userInfo = null;
		try {

			userInfo = managerService.getUserInfo(username);

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		jsonUtil.setData(userInfo);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 * 检查token是否有效
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void checkToken(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		JWTTokenUtil jwtTokenUtil = new JWTTokenUtil();

		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		try {
			String username = jwtTokenUtil.getDecodeToken(req);
			if(username != null && username.length() == 0) {
				throw new RuntimeException("请重新登录！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(401); // 身份未授权
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		out.print(gson.toJson(jsonUtil));
	}


}
